<template>
  <el-dialog :close-on-click-modal='false' :title="$t('全局配置')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang === 'zh'?'480px':'540px'" v-drag @close="close">
    <el-form ref="form" :model="record" :label-width="lang === 'zh'?'150px':'210px'" :rules="rules" label-position="left" style="margin:0 20px;">
      <el-form-item :label="$t('精细查杀')" prop="scan_packet">
        <template slot="label">{{$t('精细查杀')}}
          <el-tooltip class="item" effect="light" :content="$t('备注：开启时，将查杀每个报文，查杀速度将变慢，适用于小文件查杀。')" placement="top">
            <i class="el-icon-warning-outline" style="color:#E6A23C;font-size:16px;"></i>
          </el-tooltip>
        </template>
        <el-radio-group v-model="record.scan_packet" style="width:250px;">
          <el-radio label="on" class="on">{{$t('开启')}}</el-radio>
          <el-radio label="off" class="off">{{$t('关闭')}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="$t('扫描文件的最大大小')" prop="scan_file_max_size">
        <el-input v-emoji v-model="record.scan_file_max_size" ref="scanInput" style="width:250px;" size='small'>
          <template slot="append">{{$t('字节')}}</template>
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">{{$t('确 定')}}</el-button>
      <el-button @click="close">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getAvConfig, setAvConfig } from '@/api/safe/av'
import { inputClass } from '@/utils/input-class.js'
export default {
  name: 'globalDialog',
  data () {
    var validateSize = (rule, value, callback) => {
      if (String(value) === '') {
        callback()
      } else if (!this.isInteger(value)) {
        callback(new Error(this.$t('只能输入整数')))
      } else if (Number(value) < 524288 || Number(value) > 10485760) {
        callback(new Error(this.$t('值的范围是524288~10485760字节')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      lang: this.$i18n.locale,
      record: {
        scan_packet: 'off',
        scan_file_max_size: '1048576'
      },
      recordCopy: {
        scan_packet: 'off',
        scan_file_max_size: '1048576'
      },
      rules: {
        scan_file_max_size: [{ required: false, validator: validateSize, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init () {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['scanInput']
        inputClass(this.$refs, data)
      })
      getAvConfig().then(res => {
        if (res.status === 0) {
          this.record.scan_packet = res.info[0].scan_packet
          this.record.scan_file_max_size = res.info[0].scan_file_max_size
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          setAvConfig(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('全局配置设置成功'),
                type: 'success'
              })
              this.close()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    isInteger (value) {
      return (!isNaN(value) && (parseInt(value).toString().length === String(Number(value)).length))
    }
  }
}
</script>
<style lang="less" scoped>
.el-form-item__content {
  line-height: 28px;
}
.el-form-item {
  margin-bottom: 0;
}
</style>
